package net.udp;

import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
import java.net.SocketException;
import java.net.UnknownHostException;

/**
 * UDP聊天的服务端
 * @author Administrator
 *
 */
public class UDPChatServer {

	
	public static void main(String[] args) throws IOException {
		
		//创建接收的Socket
		DatagramSocket socket = new DatagramSocket(8888, InetAddress.getByName("localhost"));
		
		// 接收数据
		while(true) {
			// 构建包
			byte[] bytes = new byte[1024];
			DatagramPacket packet = new DatagramPacket(bytes, 0, bytes.length);
			
			// 接收数据
			socket.receive(packet);
			
			// 输出数据
			String content = new String(bytes, packet.getOffset(), packet.getLength());
			System.out.println("接收到的数据是：" + content);
		}
		
//		socket.close();
		
	}
	
	
}
